package com.chen.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.chen.base.PageResponse;
import com.chen.base.ResponseResult;
import com.chen.dto.ReservationDto;
import com.chen.service.ReservationService;
import com.chen.utils.ObjectUtil;
import com.chen.vo.ReservationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 预约管理
 */
@RestController
@RequestMapping("/reservation")
public class ReservationController extends BaseController {


    @Resource
    private ReservationService reservationService;

    /**
     * 新增预约
     * @param dto
     * @return
     */
    @PostMapping
    public ResponseResult add(@RequestBody ReservationDto dto) {
        reservationService.add(dto);
        return success();
    }

    /**
     * 更新预约
     * @param id
     * @param dto
     * @return
     */
    @PutMapping("/{id}")
    public ResponseResult update(@PathVariable Long id, @RequestBody ReservationDto dto) {
        reservationService.update(id, dto);
        return success();
    }

    /**
     * 删除预约
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseResult deleteById(@PathVariable Long id) {
        reservationService.deleteById(id);
        return success();
    }

    /**
     * 取消预约
     * @param id id
     * @return
     */
    @PutMapping("/{id}/cancel")

    public ResponseResult cancel(@PathVariable Long id) {
        reservationService.cancelReservation(id);
        return success();
    }

    /**
     * 预约到访
     * @param id id
     * @param time 时间
     * @return
     */
    @PutMapping("/{id}/visit")
    public ResponseResult visit(@PathVariable Long id, @RequestParam(required = false) Long time) {
        reservationService.visit(id, time);
        return success();
    }

    /**
     * 根据ID查询预约
     * @param id id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult<ReservationVo> findById(@PathVariable Long id) {
        ReservationVo reservationVO = reservationService.findById(id);
        return success(reservationVO);
    }

    /**
     * 查询所有预约
     * @param mobile 手机号
     * @param time 时间
     * @return
     */
    @GetMapping
    public ResponseResult<List<ReservationVo>> findAll(@RequestParam(required = false) String mobile, @RequestParam(required = false) Long time) {
        List<ReservationVo> reservationVoList = reservationService.findAll(mobile, LocalDateTimeUtil.of(time));
        return success(reservationVoList);
    }

    /**
     * 查询每个时间段剩余预约次数
     * @param time
     * @return
     */
    @GetMapping("/countByTime")
    public ResponseResult countReservationsForEachTimeWithinTimeRange(@RequestParam(required = false) Long time) {
        return success(reservationService.countReservationsForEachTimeWithinTimeRange(LocalDateTimeUtil.of(time)));

    }

    /**
     * 分页查询增加预约人姓名，手机号，状态，类型的查询条件
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @param name 预约人姓名
     * @param phone 手机号
     * @param status 状态
     * @param type 类型
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    @GetMapping("/page")
    public ResponseResult<PageResponse<ReservationVo>> findByPage(@RequestParam(defaultValue = "1") int pageNum,
                                                                  @RequestParam(defaultValue = "10") int pageSize,
                                                                  @RequestParam(required = false) String name,
                                                                  @RequestParam(required = false) String phone,
                                                                  @RequestParam(required = false) Integer status,
                                                                  @RequestParam(required = false) Integer type,
                                                                  @RequestParam(required = false) Long startTime,
                                                                  @RequestParam(required = false) Long endTime) {
        PageResponse<ReservationVo> byPage = reservationService.findByPage(pageNum, pageSize, name, phone, status, type, ObjectUtil.isEmpty(startTime)? null : LocalDateTimeUtil.of(startTime), ObjectUtil.isEmpty(endTime)? null : LocalDateTimeUtil.of(endTime));
        return success(byPage);
    }


}
